package com.zxy.ziems.server.utils;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 断言
 * @author 10348359
 * @create 2024-03-15 15:18
 */
public class AssertUtil {

    public static void isTrue(boolean expression, String errorMsgTemplate, Object... params) throws IllegalArgumentException {
         Assert.isTrue(expression, () -> BusinessExceptionUtil.exception(StrUtil.format(errorMsgTemplate, params)));
    }

    public static void isTrue(boolean expression) throws IllegalArgumentException {
        isTrue(expression, "[Assertion failed] - this expression must be true");
    }

    public static void isFalse(boolean expression, String errorMsgTemplate, Object... params) throws IllegalArgumentException {
        Assert.isFalse(expression, () -> BusinessExceptionUtil.exception(StrUtil.format(errorMsgTemplate, params)));
    }

    public static void isFalse(boolean expression) throws IllegalArgumentException {
        isFalse(expression, "[Assertion failed] - this expression must be false");
    }


    public static void isNull(Object object, String errorMsgTemplate, Object... params) throws IllegalArgumentException {
        Assert.isNull(object, () -> BusinessExceptionUtil.exception(StrUtil.format(errorMsgTemplate, params)));
    }

    public static void isNull(Object object) throws IllegalArgumentException {
        isNull(object, "[Assertion failed] - the object argument must be null");
    }

    public static <T> T nonNull(T object, String errorMsgTemplate, Object... params) throws IllegalArgumentException {
        return Assert.notNull(object, () -> BusinessExceptionUtil.exception(StrUtil.format(errorMsgTemplate, params)));
    }

    public static <T> T nonNull(T object) throws IllegalArgumentException {
        return nonNull(object, "[Assertion failed] - this argument is required; it must not be null");
    }
}
